//
// Created by Administrator on 2022/2/5.
//

#ifndef CPP_LEARNING_LEETCODE968_H
#define CPP_LEARNING_LEETCODE968_H
//
#include "treenode.h"
// 根据两个子节点的状态传递
// state -1 null 节点
// state 1  自己本身有监听器
// stage 0   自己本身没有监听器


class Solution968 {
private:
    int monitorNum = 0;
    TreeNode* root ;
public:
    int postOrder(TreeNode* node){
        if(node == nullptr) return -1;
        int leftState = postOrder(node->left);
        int rightState = postOrder(node->right);
        //  0 0    0 1  0 -1
        if(leftState == 0 || rightState == 0){
            monitorNum ++;
            return 1;
        }else if(leftState == -1 && rightState == -1){
            // 叶子节点  为root节点
            if(node == root){
                monitorNum ++;
                return 1;
            }else{
                return 0;
            }
        }else {            //   0 1    0  -1
            return 0;
        }
    }
    int minCameraCover(TreeNode* root) {
        this->root = root;
        postOrder(root);
        return monitorNum;
    }
};

void test968(){
    TreeNode* node1 = new TreeNode(1);
    TreeNode* node2 = new TreeNode(2);
    TreeNode* node3 = new TreeNode(3);
    TreeNode* node4 = new TreeNode(4);

    node1->left = node2;
    node2->left = node3;
    node2->right = node4;

    Solution968 s;
    int num = s.minCameraCover(node1);

}
#endif //CPP_LEARNING_LEETCODE968_H
